//
//  RangeSliderTrackLayer.swift
//  SwiftyRangeSlider
//
//  Created by zhuxuhong on 2022/2/24.
//

import UIKit

class RangeSliderTrackLayer: CALayer {
    weak var slider: RangeSlider?
    
    override func draw(in ctx: CGContext) {
        guard let slider = slider else {
            return
        }
        
        let thumbImageHalfWidth = slider.thumbImage.size.width/2
        let path = UIBezierPath(roundedRect: bounds.insetBy(dx: thumbImageHalfWidth, dy: 0), cornerRadius: cornerRadius)
        ctx.setFillColor(slider.trackColor.cgColor)
        ctx.addPath(path.cgPath)
        ctx.fillPath()
        
        let lowerX = slider.positionForValue(slider.lowerValue) + thumbImageHalfWidth
        let upperX = slider.positionForValue(slider.upperValue) + thumbImageHalfWidth
        let rect = bounds.inset(by: UIEdgeInsets(top: 0, left: lowerX, bottom: 0, right: bounds.width-upperX))
        ctx.setFillColor(slider.trackTintColor.cgColor)
        ctx.fill(rect)
    }
}
